$(document).ready(function(){
	//验证邮箱
    $("#reg_email").keyup(function(){
        $(this).blur(function(){
			hideErrorWithRemoveClass("reg_mailNumError","&nbsp;","reg_email");
            var mailNum=$(this).val();
            if(mailNum==""){
				showErrorWithAddClass("reg_mailNumError","请输入邮箱号","reg_email");
                return false;
            }
            if(!mailNum.match(/^[0-9a-zA-Z_-]+@+[0-9a-zA-Z_-]+.+[0-9a-zA-Z]$/)){
				showErrorWithAddClass("reg_mailNumError","请输入正确的邮箱格式","reg_email");
                return false;
            }

            $.ajax({
                url:"emailUnique",
                data:{mailNumber:mailNum},
                dataType:'json',
                type:'post',
                success:function(data){
                    if(data.status==1){
						showErrorWithAddClass("reg_mailNumError","邮箱已存在","reg_email");
                        return false;
                    }
                }
            });
        });
    });

    //点击获取验证码main
    $("#getCode").click(function(){
        var mailNumber=$("#reg_email,#findPsw_email").val();
        var error=$(".reg-wrong-tishi,.findpsw-wrong-tishi").val();
        if(mailNumber==""){
            showErrorWithAddClass("reg_mailNumError","请输入邮箱","reg_email");
            showErrorWithAddClass("findPsw_email_error","请输入邮箱","findPsw_email");
            return false;
        }
        if(error!=""){
            return false;
        }
        $.ajax({
            url:"sendCode",
            dataType:'json',
            data:{mailNum:mailNumber},
            type:'get',
            success:function(data){
                if(data.status==1){
                    getCodeTime($("#getCode"));
                }else{
                    showErrorWithAddClass("reg_mailCodeError","验证码发送失败，请重新发送","reg_emailCode");
                    showErrorWithAddClass("findPsw_email_error","验证码发送失败，请重新发送","findPsw_email");
                    return false;
                }
            }
        });
    });

	//判断验证码是否一致--注册，修改密码
    // $("#reg_emailCode,#findPsw_emailCode").keyup(function(){
        $("#reg_emailCode,#findPsw_emailCode").blur(function(){
            hideErrorWithRemoveClass("reg_mailCodeError","&nbsp;","reg_emailCode");
            hideErrorWithRemoveClass("findPsw_emailCode_error","&nbsp;","findPsw_emailCode");
            var mailIdentify=$(this).val();
            if(mailIdentify == ""){
                $(this).css("border-color","#e6e6e6");
                return false;
            }
            $.ajax({
                url:"judgeCode",
                dataType:"json",
                type:"get",
                data:{mailCode:mailIdentify},
                success:function(data){
                    if(data.status===1){
						hideErrorWithRemoveClass("reg_mailCodeError","&nbsp;","reg_emailCode");
                        hideErrorWithRemoveClass("findPsw_emailCode_error","&nbsp;","findPsw_emailCode");
                        return false;
                    }if(data.status===0){
                        showErrorWithAddClass("reg_mailCodeError","验证码不正确","reg_emailCode");
                        showErrorWithAddClass("findPsw_emailCode_error","验证码不正确","findPsw_emailCode");
                        return false;
                    }if(data.status===2){
                        showErrorWithAddClass("reg_mailCodeError","请获取验证码","reg_emailCode");
                        showErrorWithAddClass("findPsw_emailCode_error","请获取验证码","findPsw_emailCode");
                        return false;
                    }
                }
            });
        });
    // });

	//验证密码格式--注册，修改密码
    $("#reg_psw,#findPsw_logPsw").keyup(function(){
        $(this).blur(function(){
			hideErrorWithRemoveClass("reg_pswError","&nbsp;","reg_psw");
            hideErrorWithRemoveClass("findPsw_logPsw_error","&nbsp;","findPsw_logPsw");
            var psw=$(this).val();
            if(psw==""){
				showErrorWithAddClass("reg_pswError","请输入密码","reg_psw");
                showErrorWithAddClass("findPsw_logPsw_error","请输入密码","findPsw_logPsw");
                return false;
            }
            if(!psw.match(/^(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])[a-zA-Z0-9]{6,20}$/)){//数字，英文大小写
                showErrorWithAddClass("reg_pswError","密码不正确(数字+英文大小写)","reg_psw");
                showErrorWithAddClass("findPsw_logPsw_error","密码不正确(数字+英文大小写)","findPsw_logPsw");
                return false;
            }
        });
    });

	//点击注册
    $("#regBtn").click(function(){
		$(".reg-wrong-tishi").css("border-color","#e6e6e6");

        var reg_email=$("#reg_email").val();
        var reg_emailNum=$("#reg_emailCode").val();
        var reg_psw=$("#reg_psw").val();
        var reg_rePsw=$("#reg_rePsw").val();
        var reg_psw_md5=hex_md5(reg_psw);
        var error=$(".reg-wrong-tishi").html();

        var userProtcol_checked=$("#userProtcol_checkbox").attr("checked");
        if(error!="&nbsp;"){
            return false;
        }
        if(reg_email==""){
			showErrorWithAddClass("reg_mailNumError","请输入邮箱号","reg_email");
			return false;
		}
        if(reg_emailNum==""){
            showErrorWithAddClass("reg_mailCodeError","请输入验证码","reg_emailCode");
            return false;
        }
        if(reg_psw==""){
            showErrorWithAddClass("reg_pswError","请输入密码","reg_psw");
            return false;
        }
        if(reg_rePsw==""){
            showErrorWithAddClass("reg_rePswError","请输入密码","reg_rePsw");
            return false;
        }
        if(reg_psw!==reg_rePsw){
            showErrorWithAddClass("reg_rePswError","密码不一致","reg_rePsw");
            return false;
        }
        if(userProtcol_checked!="checked"){
            showErrorWithAddClassNoBorder("reg_rePswError","请同意《优得用户协议》");
            return false;
        }

        $.ajax({
            url:"registerAjax",
            data:{regEmail:reg_email,regPsw:reg_psw_md5,regRole:0},
            dataType:'json',
            type:'post',
            success:function(data){
                if(data.status==1){//注册成功
                    $(".bg").hide();
                    $(".registerSuccess-tishi").show();
                    return false;
                }else{
                	showErrorWithAddClassNoBorder("reg_mailNumError","邮箱或密码不正确");
                	return false;
                }
            }
        });
    });
	
	//登陆-验证码
	$("#log_identify").blur(function(){
		hideErrorWithRemoveClass("log_identify_error","&nbsp;","log_identify");
		var log_identify=$(this).val();
		if(log_identify==""){
			return false;
		}
		//图片验证码
		$.ajax({
			url:"checkIdentify",
			data:{identify:log_identify},
			dataType:"json",
			type:"post",
			success:function(data){
				if(data.status==1){
					// hideErrorWithRemoveClass("log_identify_error","&nbsp;","log_identify");
				}else{
					showErrorWithAddClass("log_identify_error","验证码不正确","log_identify");
					return false;
				}
			}
		});
	});

	//点击登录
	$("#loginBtn").click(function(){

		var log_email=$("#log_email").val();
		var log_psw=$("#log_psw").val();
		var log_psw_md5=hex_md5(log_psw);
		var log_identify=$("#log_identify").val();
		var log_email_error=$("#log_email_error").html();
		var log_identify_error=$("#log_identify_error").html();
		
		if(log_email_error!="&nbsp;" || log_identify_error!="&nbsp;"){
			return false;
		}
		if(log_email==""){
			showErrorWithAddClass("log_email_error","请输入邮箱号","log_email");
			return false;
		}
		if(log_psw==""){
			showErrorWithAddClass("log_psw_error","请输入密码","log_psw");
			return false;
		}
		if(log_identify==""){
			showErrorWithAddClass("log_identify_error","请输入验证码","log_identify");
			return false;
		}

		$.ajax({
			url:"loginAjax",
			data:{email:log_email,psw:log_psw_md5},
			dataType:"json",
			type:"post",
			success:function(data){
				if(data.status==1){
					hideErrorWithRemoveClassByClass("log_error","&nbsp;","log_input");

					var url=document.referrer;
                    var path = url.match("register");
                    var path2 = url.match("login");
                    var path3 = url.match("findPassword");
					if(path == "register" || path2 == "login" || path3 == "findPassword"){
						window.location.href="main.html";
					}else{
						self.location=document.referrer;//返回上一页并刷新
					}	
				}else{
					showErrorWithAddClass("log_email_error","用户名或密码错误","log_email");
					return false;
				}
			}
		});
	});

    //修改密码-验证邮箱是否存在
    $("#findPsw_email").keyup(function(){
        $(this).blur(function(){
            hideErrorWithRemoveClass("findPsw_email_error","&nbsp;","findPsw_email");
            var mailNum=$(this).val();
            if(mailNum==""){
                showErrorWithAddClass("findPsw_email_error","请输入邮箱号","findPsw_email");
                return false;
            }
            if(!mailNum.match(/^[0-9a-zA-Z_-]+@+[0-9a-zA-Z_-]+.+[0-9a-zA-Z]$/)){
                showErrorWithAddClass("findPsw_email_error","请输入正确的邮箱格式","findPsw_email");
                return false;
            }

            $.ajax({
                url:"emailUnique",
                data:{mailNumber:mailNum},
                dataType:'json',
                type:'post',
                success:function(data){
                    if(data.status==0){
                        showErrorWithAddClass("findPsw_email_error","邮箱不存在","findPsw_email");
                        return false;
                    }
                }
            });
        });
    });
    
    //修改密码
    $("#findPswSubmitBtn").click(function(){

        // $(".findpsw-wrong-tishi").css("border-color","#e6e6e6");

        var email=$("#findPsw_email").val();
        var emailCode=$("#findPsw_emailCode").val();
        var logPsw=$("#findPsw_logPsw").val();
        var logRePsw=$("#findPsw_logRePsw").val();
        var logPsw_md5=hex_md5(logPsw);
        // var error=$(".findpsw-wrong-tishi").html();
        // alert(error);
        var userProtcol_checked=$("#userProtcol_checkbox_findPsw").attr("checked");
        // if(error!="&nbsp;"){
        //     return false;
        // }
        if(email==""){
            showErrorWithAddClass("findPsw_email_error","请输入邮箱号","findPsw_email");
            return false;
        }
        if(emailCode==""){
            showErrorWithAddClass("findPsw_emailCode_error","请输入验证码","findPsw_emailCode");
            return false;
        }
        if(logPsw==""){
            showErrorWithAddClass("findPsw_logPsw_error","请输入密码","findPsw_logPsw");
            return false;
        }
        if(logRePsw==""){
            showErrorWithAddClass("findPsw_logRePsw_error","请输入密码","findPsw_logRePsw");
            return false;
        }
        if(logPsw!=logRePsw){
            showErrorWithAddClass("findPsw_logRePsw_error","密码不一致","findPsw_logRePsw");
            return false;
        }
        if(userProtcol_checked!="checked"){
            showErrorWithAddClassNoBorder("findPsw_logRePsw_error","请同意《优得用户协议》");
            return false;
        }
        $.ajax({
            url:"findPswAjax",
            data:{email:email,logPsw:logPsw_md5},
            dataType:'json',
            type:'post',
            success:function(data){
                if(data.status==1){//修改密码成功
                    location.href="login.html";
                }else{
                    hideErrorWithRemoveClassByClass("findpsw-wrong-tishi","&nbsp;","findPsw-input");
                    showErrorWithAddClassNoBorder("findPsw_email_error","重置登录密码失败");
                    return false;
                }
            }
        });
    });


	//退出--本页面
	$("#exitUser_reload").click(function(){
		$.ajax({
			url:"exitUser",
			success:function(){
				location.reload();
			}
		});
	});

});

//获取验证码 按钮
var wait=60;
function getCodeTime(val){
    if (wait == 0) {
        val.attr("disabled",false);
        val.css("background-color","#eb884b");
        val.val("获取验证码");
        wait = 60;
    } else {
        val.attr("disabled",true);
        val.css("background-color","lightgray");
        val.val( wait + "s");
        wait--;
        setTimeout(function() {
            getCodeTime(val)
        },1000);
    }
}

/*显示错误信息*/
function showErrorWithAddClass(id1,html1,id2){
    $("#"+id1).show();
    $("#"+id1).html(html1);
    $("#"+id2).css("border-color","#ff0000");
}

function showErrorWithAddClassNoBorder(id1,html1){
    $("#"+id1).show();
    $("#"+id1).html(html1);
}

/*隐藏错误信息*/
function hideErrorWithRemoveClass(id1,html1,id2){
    $("#"+id1).hide();
    $("#"+id1).html(html1);
    $("#"+id2).css("border-color","#e6e6e6");
}

function hideErrorWithRemoveClassByClass(class1,html1,class2){
    $("."+class1).hide();
    $("."+class1).html(html1);
    $("."+class2).css("border-color","#e6e6e6");
}